package com.maxus.mini.data.provider.topic;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.maux.mini.data.api.topic.TopicItemService;
import com.maxus.mini.common.ElumClass.EntityState;
import com.maxus.mini.common.utils.BeanUtilsExtended;
import com.maxus.mini.common.PageImplE;
import com.maxus.mini.data.provider.BaseServiceImpl;
import com.maxus.mini.entity.topic.Topic;
import com.maxus.mini.entity.topic.TopicItem;
import com.maxus.mini.entity.vo.topic.TopicItemVo;
import com.maxus.mini.repository.topic.TopicItemRepository;

/**
 * 专题管理
 * 
 * @author wujinsong 
 *
 */
@Component()
@Service(version = "1.0.0")
public class TopicItemServiceImpl extends BaseServiceImpl<TopicItem, Serializable>
		implements TopicItemService<TopicItem, Serializable> {
	@Autowired
	private TopicItemRepository<Topic> topicItemRepository;

	@Override
	public PageImplE<TopicItem> page(final TopicItemVo vo) {
		Page<TopicItem> page = topicItemRepository.findAll(new Specification<TopicItem>() {

			@Override
			public Predicate toPredicate(Root<TopicItem> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<>();
				list.add(cb.equal(root.<String>get("entityState"), EntityState.N.name()));

				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		}, vo);

		PageImplE<TopicItem> pageImplE = new PageImplE<>();
		BeanUtilsExtended.copyProperties(pageImplE, page);

		return pageImplE;
	}
}
